package com.nondo.dean.platform.controller.api.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.ProductDiscount;
import com.nondo.dean.platform.model.result.ProductDiscountResult;
import com.nondo.dean.platform.service.ProductDiscountService;

/**
 * 产品折扣率管理后台接口
 * @author zlj
 * @date 2018年4月3日
 */
@RestController("apiAdminProductDiscount")
@RequestMapping("/{v}/admin/price")
public class ProductDiscountController {
    
    @Autowired
    ProductDiscountService productDiscountService;
    
    /**
     * 查看产品折扣率详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<ProductDiscountResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<ProductDiscountResult> view = new View<ProductDiscountResult>(20000, "获取数据成功");
        
        ProductDiscountResult productDiscountResult = null;
        if (id != null) {
            ProductDiscount productDiscountDb = productDiscountService.selectProductDiscount(id);
            productDiscountResult = productDiscountService.getProductDiscountResult(productDiscountDb);
        } else {
            productDiscountResult = new ProductDiscountResult();
        }

        view.setData(productDiscountResult);
        return view;
    }
    
    /**
     * 添加产品折扣率
     * @param request
     * @param response
     * @param productDiscount 产品折扣率信息 {@link ProductDiscount}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ProductDiscount> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ProductDiscount productDiscount, BindingResult result) {
        View<ProductDiscount> view = new View<ProductDiscount>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Integer userId = productDiscount.getUserId();
            Integer productId = productDiscount.getProductId();
            ProductDiscount productDiscountDb = this.productDiscountService.getProductDiscountByUserAndProductId(userId,productId);
            if (productDiscountDb != null) {
                return View.of500Error("已对该经销商设置产品折扣");
            }
            this.productDiscountService.addProductDiscount(productDiscount);
        }
        return view;
    }

    /**
     * 更新产品折扣率
     * @param request
     * @param response
     * @param productDiscount 产品折扣率信息 {@link ProductDiscount}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ProductDiscount> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ProductDiscount productDiscount, BindingResult result) {
        View<ProductDiscount> view = new View<ProductDiscount>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Integer userId = productDiscount.getUserId();
            Integer productId = productDiscount.getProductId();
            ProductDiscount productDiscountDb = this.productDiscountService.getProductDiscountByUserAndProductId(userId,productId);
            if ( productDiscountDb!=null && productDiscountDb.getId() != productDiscount.getId()) {
                return View.of500Error("已对该经销商设置产品折扣");
            }
            this.productDiscountService.updateProductDiscount(productDiscount);
        }

        return view;
    }

    /**
     * 获取产品折扣率分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<ProductDiscountResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductDiscount> query) {
        View<Page<ProductDiscountResult>> view = new View<Page<ProductDiscountResult>>(20000, "获取列表成功");

        ProductDiscount productDiscount = query.getData();

        Page<ProductDiscountResult> page = new Page<ProductDiscountResult>(query.getPage(), query.getLimit(), productDiscountService.count(productDiscount));
        
        List<ProductDiscount> productDiscountList = productDiscountService.getProductDiscountList(query.getOffset(), query.getLimit(), productDiscount);
        
        List<ProductDiscountResult> resultList = new ArrayList<ProductDiscountResult>();
        
        for (ProductDiscount productDiscountDb : productDiscountList) {
            ProductDiscountResult productDiscountResult = new ProductDiscountResult();
            productDiscountResult = productDiscountService.getProductDiscountResult(productDiscountDb);
            resultList.add(productDiscountResult);
        }
        
        page.setContent(resultList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除产品折扣率
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        ProductDiscount productDiscount = productDiscountService.selectProductDiscount(id);
        productDiscount.setDelFlag(String.valueOf(1));
        this.productDiscountService.updateProductDiscount(productDiscount);

        return view;
    }
    
}